package testing;

import org.jfugue.devtools.DiagnosticParserListener;
import org.jfugue.integration.MusicXmlParser;
import org.jfugue.midi.MidiFileManager;
import org.jfugue.pattern.Pattern;
import org.jfugue.pattern.Token;
import org.jfugue.player.Player;
import org.jfugue.theory.ChordProgression;
import org.jfugue.theory.Note;

import java.io.File;
import java.io.IOException;
import java.time.LocalDateTime;

public class JfugueMain {
    public static void main(String...args) throws IOException {
        Player player = new Player();
//        Pattern song = new Pattern()
//                .setTempo(160)
//                .add("V0 I[Piano] C5q D5q E5q F5h") // 主旋律
//                .add("V1 I[Cello] G3q Ri A3q Ri");   // 低音伴奏
//        StringBuilder melody = new StringBuilder();
//        String[] notes = {"C", "D", "E", "F", "G"};
//        for (int i=0; i<8; i++) {
//            melody.append(notes[(int)(Math.random()*notes.length)]).append("5q ");
//        }
//        player.play("T80 V0 I[Piano] A5q C6i E5q G5i F5q A5i E5h A4minw T80 V1 I[Strings_Ensemble] A3w G3w F3w G3w T80 V2 I[Drums] Bass_Drum5q Closed_Hi-Hat5i Snare5q Closed_Hi-Hat5i T80 V3 I[Glockenspiel] C7s E7s G7s");
//        player.play("T120 V0 I[Piano] A5q C6i E5q G5i F5q A5i E5h V1 I[Flute] A3w G3w F3w G3w");
//        player.play("T120 V0 I0 A5q C6i E5q G5i F5q A5i E5h | V1 I72 A3w G3w F3w G3w");
//        player.play("T120 V0 I74 C5q E5q G5i F5q E5q D5i C5h G5q F5i E5q D5q C5i G4h ",
//                "V1 I24 C4majh G4majq | B3majh F4majq | A3minh E4majq | D4minh G4sus2q ",
//                "V2 I0 E4q C4i G4q E4i F4q D4i A4q F4i | C4q G3i E4q C4i D4q A3i F4q D4i ",
//                "V3 I48 C3w G2w F2w C3w E3w B2w D3w G2w ",
//                "V4 I33 C3q E3i G3q B3i F3q A3i E3q G3i");
//        player.play("T120 V0 I0 C5q E5i G5q F5i E5q D5i C5h | E5q G5i A5q G5i F5q E5i D5h | C5q E5i G5q F5i E5q D5i C5h  V1 I40 C4majw F4majw G4majw F4majw | C4majw F4majw G4majw C4majw V2 I43 C3h. G2q C3h. G2q | F3h. C3q F3h. C3q");
//            player.play("V0 I0 T76 C5q E5q G5q E5q C5q E5q G5q E5q G5q B5q D6q B5q G5q B5q D6q B5q C5q E5q G5q E5q C5q E5q G5q E5q G5i B5i D6q B5q G5i B5i D6q B5q C5s E5s G5s E5s C5s E5s G5s E5s C5s E5s G5s E5s C5s E5s G5s E5s ");
//        Player player = new Player();
        /*Pattern pattern = new Pattern("T120 " +
                "V0 I0 C5q E5q G5q C6q E5q G5q C6i B5i A5h G5q B5q D6q G5q B5q D6i C6i B5h C5q E5q G5q C6q E5q G5q C6i B5i A5h F5q A5q C6q F5q A5q C6i B5i A5h C5q E5q G5q C6q E5q G5q C6i B5i A5h G5q B5q D6q G5q B5q D6i C6i B5h A5q C6q E6q A5q C6q E6i D6i C6h G5q B5q D6q G5q B5q D6i C6i B5h C5q E5q G5q C6q E5q G5q C6i B5i A5h F5q A5q C6q F5q A5q C6i B5i A5h C5q E5q G5q C6q E5h G5w C5q E5q G5q C6q E5h G5w " +
                "V1 I43 C3w G2w A2w F2w C3w G2w A2w F2w C3w G2w A2w F2w C3w C3w G2w G2w C3w G2w D3w G2w F2w C2w C3q G3q C3q G3q C3w _w " +
               "V2 I48 E4q G4q B4q E4q C4q E4q G4q C5q A4q C5q E5q A4q F4q A4q C5q F4q E4q G4q C5q E4q C4q E4q G4q C5q A4q C5q E5q A4q F4q A4q C5q F4q E4q G4q B4q E4q D4q F4q A4q D4q G4q B4q D5q G4q E4q G4q B4q E4q C4q E4q G4q C5q G4q B4q D5q G4q C5h E5q G5i _i");*/

/*
        Pattern pattern = new Pattern("T120 " +
                "V0 I0 C5q E5q G5q C6q E5q G5q C6i B5i A5h G5q B5q D6q G5q B5q D6i C6i B5h C5q E5q G5q C6q E5q G5q C6i B5i A5h F5q A5q C6q F5q A5q C6i B5i A5h C5q E5q G5q C6q E5q G5q C6i B5i A5h G5q B5q D6q G5q B5q D6i C6i B5h A5q C6q E6q A5q C6q E6i D6i C6h G5q B5q D6q G5q B5q D6i C6i B5h C5q E5q G5q C6q E5q G5q C6i B5i A5h F5q A5q C6q F5q A5q C6i B5i A5h C5q E5q G5q C6q E5h G5w C5q E5q G5q C6q E5h G5w " +
                "V1 I43 C3w G2w A2w F2w C3w G2w A2w F2w C3w G2w A2w F2w C3w G2w A2w F2w C3w G2w D3w G2w C3w G2w C3w G2w " +
                "V2 I48 E4q G4q B4q E4q C4q E4q G4q C5q A4q C5q E5q A4q F4q A4q C5q F4q E4q G4q B4q E4q C4q E4q G4q C5q A4q C5q E5q A4q F4q A4q C5q F4q E4q G4q B4q E4q D4q F#4q A4q D4q G4q B4q D5q G4q E4q G4q B4q E4q C4q E4q G4q C5q A4q C5q E5q A4q F4q A4q C5q F4q E4q G4q B4q E4q C4q E4q G4q C5q");
*/

        Pattern pattern = new Pattern("T90 V0 I[Piano] C5q E5i G5q B4i A5q C6i G5q E5i F5h A5q G5w " +
                "V1 I[Cello] C3w G3h F3q A3h E3w G3q " +
                "V2 I[Guitar] C4q E4i G4q C4i E4i G4q B3i D4q G4i B3q D4i");

//        player.play(pattern);
//        player.play("T120 " +
//                "V0 I0 C5q E5q G5q C6q E5q G5q C6i B5i A5h G5q B5q D6q G5q B5q D6i C6i B5h C5q E5q G5q C6q E5q G5q C6i B5i A5h F5q A5q C6q F5q A5q C6i B5i A5h C5q E5q G5q C6q E5q G5q C6i B5i A5h G5q B5q D6q G5q B5q D6i C6i B5h A5q C6q E6q A5q C6q E6i D6i C6h G5q B5q D6q G5q B5q D6i C6i B5h C5q E5q G5q C6q E5q G5q C6i B5i A5h F5q A5q C6q F5q A5q C6i B5i A5h C5q E5q G5q C6q E5h G5w C5q E5q G5q C6q E5h G5w " +
//                "V1 I43 C3w G2w A2w F2w C3w G2w A2w F2w C3w G2w A2w F2w C3w C3w G2w G2w C3w G2w A2w F2w C3w G2w D3w G2w C3w G2w C3w G2w " +
//                "V2 I48 E4q G4q B4q E4q C4q E4q G4q C5q A4q C5q E5q A4q F4q A4q C5q F4q E4q G4q C5q E4q C4q E4q G4q C5q A4q C5q E5q A4q F4q A4q C5q F4q E4q G4q B4q E4q D4q F4q A4q D4q G4q B4q D5q G4q E4q G4q B4q E4q C4q E4q G4q C5q A4q C5q E5q A4q F4q A4q C5q F4q");

//        MidiFileManager.savePatternToMidi(pattern, new File("output0.mid"));


//        player.play("I0 T60 V0 F4q Ab4q Bb4q C5q Bb4q Ab4q G4q F4q D4q.V F4i G4q.V Ab4i F4i G4i F4q.V Eb5q G4q F4q D4q.V F4i G4q.V Ab4i F4i G4i F4q.V Eb5q G4q F4q F4q Ab4q Bb4q C5q Bb4q Ab4q G4q F4h.");
//        player.play("T60 V0 i[Piano] C4q G4q Eb5q F4h | A4i Bb4q C5i Eb5h | D4q F4i G4q.s C5i Bb4i | A4q. Eb5i F4q G4h | C4w :CON(10,80) (C4+Eb4+G4)q (D4+F4+A4)q (Eb4+G4+Bb4)h | D4q.ri F4s G4s A4i Bb4q | C5i G4s Eb5s F4q G4q.s | A4i Bb4i C5q.ri G4h | C4q G4q Eb5q F4h :CON(10,100) (C4+Eb4)w");
//        player.play("T60 I[Piano] C5q E♭5q G5q | D5q. D5· D5q | F5q. F5· F5q | G5q. G5· G5q | A♭5q B♭5q A♭5q | C5q E♭5q G5q C5h E♭5h G5h");
//        player.play(":CON(10, 90) T60 V0 I[Piano] C4q G4q Eb4q F4q C4q G4q Eb4q F4q F4q. A4i Bb4t F4t D5t D4q. F4i G4t F4t D4t C4q G4q Eb4q F4q C4q G4q Eb4q F4q F4q. A4i Bb4t F4t D5t D4q. F4i G4t F4t D4t Eb4q A4q C5q G4q F4q. Ab4i Bb4t F4t C5t C4q G4q Eb4q F4q C4q G4q Eb4q F4q F4q. A4i Bb4t F4t D5t D4q. F4i G4t F4t D4t C4q G4q Eb4q F4q C4q G4q Eb4h F4h C4w G4w Eb4w F4w");
        long start = System.currentTimeMillis();
        Pattern v0 = new Pattern("T120 V0 I[Piano] :Controller(10,90) :Controller(7,60) " +
                "F5q G5q A5q Bb5q F5q G5q A5q Bb5q F5q G5q A5q Bb5q F5q G5q A5q Bb5q " +
                "F5q G5q A5q Bb5q F5q G5q A5q Bb5q F5q G5q A5q Bb5q F5q G5q A5q Bb5q " +
                "D5q. F5s*3 G5s*3 D5q. F5s*3 G5s*3 (D5+F5)q. (A5+C6)s*3 (G5+Bb5)s*3 (D5+F5)q. (A5+C6)s*3 (G5+Bb5)s*3 " +
                "Eb5h*3 D5i F5i G5i A5h*3 G5i F5i Eb5i C6q Bb5q A5q G5q F5q G5q A5q Bb5q " +
                "C6q Bb5q A5q G5q F5q G5q A5q Bb5q (D6+Eb6)h. (C6+D6)h. (Bb5+C6)h. (A5+Bb5)h. " +
                "F5q G5q A5q Bb5q F5q G5q A5q Bb5q F5q. G5s*3 A5s*3 Bb5s*3 F5q. G5s*3 A5s*3 Bb5s*3 F5w")
                .add(":Controller(91, 40)").add(":Controller(93, 60)");
        Pattern v1 = new Pattern(
                // 伴奏音轨（左声道弦乐）
                "T48 V1 I[Orchestral_Strings] :Controller(10,10) :Controller(7,80) " +
                        // 前奏段
                        "(C3+Eb4+G4)h (C3+Eb4+G4)h (G3+Bb4+D5)h (G3+Bb4+D5)h (C3+Eb4+A4)q. A3s*3 " +
                        "(F3+Ab4+C5)h (F3+Ab4+C5)h (C3+Eb4+G4)h (C3+Eb4+G4)h (G3+Bb4+E5)q. E3s*3 " +
                        // 间奏段
                        "C4w (Eb4+G4+Bb4)q (Eb4+G4+Bb4)q (Eb4+G4+Bb4)q (Eb4+G4+Bb4)q " +
                        "C4w (F4+Ab4+C5)q (F4+Ab4+C5)q (F4+Ab4+C5)q (F4+Ab4+C5)q " +
                        "C4w (D4+F4+A4)q (D4+F4+A4)q (D4+G4+Bb4)q (D4+G4+Bb4)q " +
                        // 高潮段
                        "(G3+Bb4+D5+F5)h (G3+Bb4+D5+F5)h (C4+Eb4+G4+Bb4)h (C4+Eb4+G4+Bb4)h " +
                        "(Ab3+C5+Eb5)q (Ab3+C5+Eb5)q (Ab3+C5+Eb5)q (Ab3+C5+Eb5)q " +
                        // 结尾段
                        ":Controller(1,80) (G2+B3+D4+F4)h. :Controller(7,50) (G2+B3+D4)h. :Controller(7,30) (G2+B3)w")
                .add(":Controller(91, 30)")  // 混响强度
                .add(":Controller(93, 40)"); // 合唱强度;
//        Pattern v0 = new Pattern("T60 V0 I0 C5q~ E5q.> G5q.> Ab5q.> | D5q.> F5q.> D5q.> Eb5q.> | C5q.> B4q.> A4q.> G4q.> | F4q.> E4q.> D4q.> C4q.> | C5q E5q G5q C6q | C5q.> E5q.> G5q.> Ab5q.> | D5q.> F5q.> D5q.> Eb5q.> | C5q.> B4q.> A4q.> G4q.> | F4q.> E4q.> D4q.> C4q. | C5q E5q G5q C6q | C5q.> E5q.> G5q.> Ab5q.> | D5q.> F5q.> D5q.> Eb5q.> | C5q.> B4q.> A4q.> G4q. | F4q.> E4q.> D4q.> C4q. | C5q E5q G5q C6q | C5q.> E5q.> G5q.> Ab5q.> | D5q.> F5q.> D5q.> Eb5q.> | C5q.> B4q.> A4q.> G4q. | F4q.> E4q.> D4q.> C4q. | C5q E5q G5q C6q | C5q.> E5q.> G5q. Ab5q.> | D5q.> F5q.> D5q.> Eb5q.> | C5q.> B4q.> A4q.> G4q. | F4q.> E4q.> D4q.> C4q. | C5q E5q G5q C6q | C5q.> E5q.> G5q. Ab5q.> | D5q.> F5q.> D5q.> Eb5q.> | C5q.> B4q.> A4q.> G4q. | F4q.> E4q.> D4q.> C4q. | C5q E5q G5q C6q | C5q.> E5q.> G5q. Ab5q.> | D5q.> F5q.> D5q.> Eb5q.> | C5q.> B4q.> A4q.> G4q. | F4q.> E4q.> D4q.> C4q. | C5q E5q G5q C6q | C5q.> E5q.> G5q. Ab5q.> | D5q.> F5q.> D5q.> Eb5q.> | C5q.> B4q.> A4q.> G4q. | F4q.> E4q.> D4q.> C4q. | C5q E5q G5q C6q | C5q.> E5q.> G5q. Ab5q.> | D5q.> F5q.> D5q.> Eb5q.> | C5q.> B4q.> A4q.> G4q. | F4q.> E4q.> D4q.> C4q. | C5q E5q G5q C6q | C5q.> E5q.> G5q. Ab5q.> | D5q.> F5q.> D5q.> Eb5q.> | C5q.> B4q.> A4q.> G4q. | F4q.> E4q.> D4q.> C4q. | C5q E5q G5q C6q | C5q.> E5q.> G5q. Ab5q.> | D5q.> F5q.> D5q.> Eb5q.> | C5q.> B4q.> A4q.> G4q. | F4q.> E4q.> D4q.> C4q. | C5q E5q G5q C6q | C5q.> E5q.> G5q. Ab5q.> | D5q.> F5q.> D5q.> Eb5q.> | C5q.> B4q.> A4q.> G4q. | F4q.> E4q.> D4q.> C4q. | C5q E5q G5q C6q | C5q.> E5q.> G5q. Ab5q.> | D5q.> F5q.> D5q.> Eb5q.> | C5q.> B4q.> A4q.> G4q. | F4q.> E4q.> D4q.> C4q. | C5q E5q G5q C6q | C5q.> E5q.> G5q. Ab5q.> | D5q.> F5q.> D5q.> Eb5q.> | C5q.> B4q.> A4q.> G4q. | F4q.> E4q.> D4q.> C4q. | C5q E5q G5q C6q | C5q.> E5q.> G5q. Ab5q.> | D5q.> F5q.> D5q.> Eb5q.> | C5q.> B4q.> A4q.> G4q. | F4q.> E4q.> D4q.> C4q. | C5q E5q G5q C6q | C5q.> E5q.> G5q. Ab5q.> | D5q.> F5q.> D5q.> Eb5q.> | C5q.> B4q.> A4q.> G4q. | F4q.> E4q.> D4q.> C4q. | C5q E5q G5q C6q | C5q.> E5q.> G5q. Ab5q.> | D5q.> F5q.> D5q.> Eb5q.> | C5q.> B4q.> A4q.> G4q. | F4q.> E4q.> D4q.> C4q. | C5q E5q G5q C6q | C5q.> E5q.> G5q. Ab5q.> | D5q.> F5q.> D5q.> Eb5q.> | C5q.> B4q.> A4q.> G4q. | F4q.> E4q.> D4q.> C4q. | C5q E5q G5q C6q | C5q.> E5q.> G5q. Ab5q.> | D5q.> F5q.> D5q.> Eb5q.> | C5q.> B4q.> A4q.> G4q. | F4q.> E4q.> D4q.> C4q. | C5q E5q G5q C6q | C5q.> E5q.> G5q. Ab5q.> | D5q.> F5q.> D5q.> Eb5q.> | C5q.> B4q.> A4q.> G4q. | F4q.> E4q.> D4q.> C4q. | C5q E5q G5q C6q | C5q.> E5q.> G5q. Ab5q.> | D5q.> F5q.> D5q.> Eb5q.> | C5q.> B4q.> A4q.> G4q. | F4q.> E4q.> D4q.> C4q. | C5q E5q G5q C6q | C5q.> E5q.> G5q. Ab5q.> | D5q.> F5q.> D5q.> Eb5q.> | C5q.> B4q.> A4q.> G4q. | F4q.> E4q.> D4q.> C4q. | C5q E5q G5q C6q | C5q.> E5q.> G5q. Ab5q.> | D5q.> F5q.> D5q.> Eb5q.> | C5q.> B4q.> A4q.> G4q. | F4q.> E4q.> D4q.> C4q. | C5q E5q G5q C6q | C5q.> E5q.> G5q. Ab5q.> | D5q.> F5q.> D5q.> Eb5q.> | C5q.> B4q.> A4q.> G4q. | F4q.> E4q.> D4q.> C4q. | C5q E5q G5q C6q | C5q.> E5q.> G5q. Ab5q.> | D5q.> F5q.> D5q.> Eb5q.> | C5q.> B4q.> A4q.> G4q. | F4q.> E4q.> D4q.> C4q. | C5q E5q G5q C6q | C5q.> E5q.> G5q. Ab5q.> | D5q.> F5q.> D5q.> Eb5q.> | C5q.> B4q.> A4q.> G4q. | F4q.> E4q.> D4q.> C4q. | C5q E5q G5q C6q | C5q.> E5q.> G5q. Ab5q.> | D5q.> F5q.> D5q.> Eb5q.> | C5q.> B4q.> A4q.> G4q. | F4q.> E4q.> D4q.> C4q. | C5q E5q G5q C6q | C5q.> E5q.> G5q. Ab5q.> | D5q.> F5q.> D5q.> Eb5q.> | C5q.> B4q.> A4q.> G4q. | F4q.> E4q.> D4q.> C4q. | C5q E5q G5q C6q | C5q.> E5q.> G5q. Ab5q.> | D5q.> F5q.> D5q.> Eb5q.> | C5q.> B4q.> A4q.> G4q. | F4q.> E4q.> D4q.> C4q. | C5q E5q G5q C6q | C5q.> E5q.> G5q. Ab5q.> | D5q.> F5q.> D5q.> Eb5q.> | C5q.> B4q.> A4q.> G4q. | F4q.> E4q.> D4q.> C4q. | C5q E5q G5q C6q | C5q.> E5q.> G5q. Ab5q.> | D5q.> F5q.> D5q.> Eb5q.> | C5q.> B4q.> A4q.> G4q. | F4q.> E4q.> D4q.> C4q. | C5q E5q G5q C6q | C5q.> E5q.> G5q. Ab5q.> | D5q.> F5q.> D5q.> Eb5q.> | C5q.> B4q.> A4q.> G4q. | F4q.> E4q.> D4q.> C4q. | C5q E5q G5q C6q | C5q.> E5q.> G5q. Ab5q.> | D5q.> F5q.> D5q.> Eb5q.> | C5q.> B4q.> A4q.> G4q. | F4q.> E4q.> D4q.> C4q. | C5q E5q G5q C6q | C5q.> E5q.> G5q. Ab5q.> | D5q.> F5q.> D5q.> Eb5q.> | C5q.> B4q.> A4q.> G4q. | F4q.> E4q.> D4q.> C4q. | C5q E5q G5q C6q | C5q.> E5q.> G5q. Ab5q.> | D5q.> F5q.> D5q.> Eb5q.> | C5q.> B4q.> A4q.> G4q. | F4q.> E4q.> D4q.> C4q. | C5q E5q G5q C6q | C5q.> E5q.> G5q. Ab5q.> | D5q.> F5q.> D5q.> Eb5q.> | C5q.> B4q.> A4q.> G4q. | F4q.> E4q.> D4q.> C4q. | C5q E5q G5q C6q | C5q.> E5q.> G5q. Ab5q.> | D5q.> F5q.> D5q.> Eb5q.> | C5q.> B4q.> A4q.> G4q. | F4q.> E4q.> D4q.> C4q. | C5q E5q G5q C6q | C5q.> E5q.> G5q. Ab5q.> | D5q.> F5q.> D5q.> Eb5q.> | C5q.> B4q.> A4q.> G4q. | F4q.> E4q.> D4q.> C4q. | C5q E5q G5q C6q | C5q.> E5q.>");
//        Pattern v1 = new Pattern("T60 V1 I[Orchestral_Strings] C5q~ E5q.> G5q.> Ab5q.> | D5q.> F5q.> D5q.> Eb5q.> | C5q.> B4q.> A4q.> G4q.> | F4q.> E4q.> D4q.> C4q. | C5q E5q G5q C6q | C5q.> E5q.> G5q.> Ab5q.> | D5q.> F5q.> D5q.> Eb5q.> | C5q.> B4q.> A4q.> G4q.> | F4q.> E4q.> D4q.> C4q. | C5q E5q G5q C6q | C5q.> E5q.> G5q.> Ab5q.> | D5q.> F5q.> D5q.> Eb5q.> | C5q.> B4q.> A4q.> G4q. | F4q.> E4q.> D4q.> C4q. | C5q E5q G5q C6q | C5q.> E5q.> G5q.> Ab5q.> | D5q.> F5q.> D5q.> Eb5q.> | C5q.> B4q.> A4q.> G4q. | F4q.> E4q.> D4q.> C4q. | C5q E5q G5q C6q | C5q.> E5q.> G5q.> Ab5q.> | D5q.> F5q.> D5q.> Eb5q.> | C5q.> B4q.> A4q.> G4q. | F4q.> E4q.> D4q.> C4q. | C5q E5q G5q C6q | C5q.> E5q.> G5q.> Ab5q.> | D5q.> F5q.> D5q.> Eb5q.> | C5q.> B4q.> A4q.> G4q. | F4q.> E4q.> D4q.> C4q. | C5q E5q G5q C6q | C5q.> E5q.> G5q.> Ab5q.> | D5q.> F5q.> D5q.> Eb5q.> | C5q.> B4q.> A4q.> G4q. | F4q.> E4q.> D4q.> C4q. | C5q E5q G5q C6q | C5q.> E5q.> G5q.> Ab5q.> | D5q.> F5q.> D5q.> Eb5q.> | C5q.> B4q.> A4q.> G4q. | F4q.> E4q.> D4q.> C4q. | C5q E5q G5q C6q | C5q.> E5q.> G5q.> Ab5q.> | D5q.> F5q.> D5q.> Eb5q.> | C5q.> B4q.> A4q.> G4q. | F4q.> E4q.> D4q.> C4q. | C5q E5q G5q C6q | C5q.> E5q.> G5q.> Ab5q.> | D5q.> F5q.> D5q.> Eb5q.> | C5q.> B4q.> A4q.> G4q. | F4q.> E4q.> D4q.> C4q. | C5q E5q G5q C6q | C5q.> E5q.> G5q.> Ab5q.> | D5q.> F5q.> D5q.> Eb5q.> | C5q.> B4q.> A4q.> G4q. | F4q.> E4q.> D4q.> C4q. | C5q E5q G5q C6q | C5q.> E5q.> G5q.> Ab5q.> | D5q.> F5q.> D5q.> Eb5q.> | C5q.> B4q.> A4q.> G4q. | F4q.> E4q.> D4q.> C4q. | C5q E5q G5q C6q | C5q.> E5q.> G5q.> Ab5q.> | D5q.> F5q.> D5q.> Eb5q.> | C5q.> B4q.> A4q.> G4q. | F4q.> E4q.> D4q.> C4q. | C5q E5q G5q C6q | C5q.> E5q.> G5q.> Ab5q.> | D5q.> F5q.> D5q.> Eb5q.> | C5q.> B4q.> A4q.> G4q. | F4q.> E4q.> D4q.> C4q. | C5q E5q G5q C6q | C5q.> E5q.> G5q.> Ab5q.> | D5q.> F5q.> D5q.> Eb5q.> | C5q.> B4q.> A4q.> G4q. | F4q.> E4q.> D4q.> C4q. | C5q E5q G5q C6q | C5q.> E5q.> G5q.> Ab5q.> | D5q.> F5q.> D5q.> Eb5q.> | C5q.> B4q.> A4q.> G4q. | F4q.> E4q.> D4q.> C4q. | C5q E5q G5q C6q | C5q.> E5q.> G5q.> Ab5q.> | D5q.> F5q.> D5q.> Eb5q.> | C5q.> B4q.> A4q.> G4q. | F4q.> E4q.> D4q.> C4q. | C5q E5q G5q C6q | C5q.> E5q.> G5q.> Ab5q.> | D5q.> F5q.> D5q.> Eb5q.> | C5q.> B4q.> A4q.> G4q. | F4q.> E4q.> D4q.> C4q. | C5q E5q G5q C6q | C5q.> E5q.> G5q.> Ab5q.> | D5q.> F5q.> D5q.> Eb5q.> | C5q.> B4q.> A4q.> G4q. | F4q.> E4q.> D4q.> C4q. | C5q E5q G5q C6q | C5q.> E5q.> G5q.> Ab5q.> | D5q.> F5q.> D5q.> Eb5q.> | C5q.> B4q.> A4q.> G4q. | F4q.> E4q.> D4q.> C4q. | C5q E5q G5q C6q | C5q.> E5q.> G5q.> Ab5q.> | D5q.> F5q.> D5q.> Eb5q.> | C5q.> B4q.> A4q.> G4q. | F4q.> E4q.> D4q.> C4q. | C5q E5q G5q C6q | C5q.> E5q.> G5q.> Ab5q.> | D5q.> F5q.> D5q.> Eb5q.> | C5q.> B4q.> A4q.> G4q. | F4q.> E4q.> D4q.> C4q. | C5q E5q G5q C6q | C5q.> E5q.> G5q.> Ab5q.> | D5q.> F5q.> D5q.> Eb5q.> | C5q.> B4q.> A4q.> G4q. | F4q.> E4q.> D4q.> C4q. | C5q E5q G5q C6q | C5q.> E5q.> G5q.> Ab5q.> | D5q.> F5q.> D5q.> Eb5q.> | C5q.> B4q.> A4q.> G4q. | F4q.> E4q.> D4q.> C4q. | C5q E5q G5q C6q | C5q.> E5q.> G5q.> Ab5q.> | D5q.> F5q.> D5q.> Eb5q.> | C5q.> B4q.> A4q.> G4q. | F4q.> E4q.> D4q.> C4q. | C5q E5q G5q C6q | C5q.> E5q.> G5q.> Ab5q.> | D5q.> F5q.> D5q.> Eb5q.> | C5q.> B4q.> A4q.> G4q. | F4q.> E4q.> D4q.> C4q. | C5q E5q G5q C6q | C5q.> E5q.> G5q.> Ab5q.> | D5q.> F5q.> D5q.> Eb5q.> | C5q.> B4q.> A4q.> G4q. | F4q.> E4q.> D4q.> C4q. | C5q E5q G5q C6q | C5q.> E5q.> G5q.> Ab5q.> | D5q.> F5q.> D5q.> Eb5q.> | C5q.> B4q.> A4q.> G4q. | F4q.> E4q.> D4q.> C4q. | C5q E5q G5q C6q | C5q.> E5q.> G5q.> Ab5q.> | D5q.> F5q.> D5q.> Eb5q.> | C5q.> B4q.> A4q.> G4q. | F4q.> E4q.> D4q.> C4q. | C5q E5q G5q C6q | C5q.> E5q.> G5q.> Ab5q.> | D5q.> F5q.> D5q.> Eb5q.> | C5q.> B4q.> A4q.> G4q. | F4q.> E4q.> D4q.> C4q. | C5q E5q G5q C6q | C5q.> E5q.> G5q.> Ab5q.> | D5q.> F5q.> D5q.> Eb5q.> | C5q.> B4q.> A4q.> G4q. | F4q.> E4q.> D4q.> C4q. | C5q E5q G5q C6q | C5q.> E5q.> G5q.> Ab5q.> | D5q.> F5q.> D5q.> Eb5q.> | C5q.> B4q.> A4q.> G4q. | F4q.> E4q.> D4q.> C4q. | C5q E5q G5q C6q | C5q.> E5q.> G5q.> Ab5q.> | D5q.> F5q.> D5q.> Eb5q.> | C5q.> B4q.> A4q.> G4q. | F4q.> E4q.> D4q.> C4q. | C5q E5q G5q C6q | C5q.> E5q.> G5q.> Ab5q.> | D5q.> F5q.> D5q.> Eb5q.> | C5q.> B4q.> A4q.> G4q. | F4q.> E4q.> D4q.> C4q. | C5q E5q G5q C6q | C5q.> E5q.> G5q.> Ab5q.> | D5q.> F5q.> D5q.> Eb5q.> | C5q.> B4q.> A4q.> G4q. | F4q.> E4q.> D4q.> C4q. | C5q E5q G5q C6q | C5q.> E5q.> G5q.> Ab5q.> | D5q.> F5q.> D5q.> Eb5q.> | C5q.> B4q.> A4q.> G4q. | F4q.> E4q.> D4q.> C4q. | C5q E5q G5q C6q | C5q.> E5q.> G5q.> Ab5q.> | D5q.> F5q.> D5q.> Eb5q.> | C5q.> B4q.> A4q.> G4q. | F4q.> E4q.> D4q.> C4q. | C5q E5q G5q C6");
        Pattern fullScore = new Pattern()
                .add(v0)
                .add(v1);
//        Pattern fullScore = new Pattern("V0 I[Piano] :Controller(10,90) :Controller(7,60) " +
//                // 主旋律优化(调整为Bb大调框架)
//                "F5q G5q A5q Bb5q | F5q G5q A5q Bb5q | F5q G5q A5q Bb5q | F5q G5q A5q Bb5q | " +
//                "D5q F5i G5i A5i Bb5i | D5q F5i G5i A5i Bb5i | (D5+F5)h (A5+C6)q (G5+Bb5)q | (D5+F5)h (A5+C6)q (G5+Bb5)q | " +
//                "Eb5w | D5q F5q G5q A5q | G5q F5q Eb5q C6q | Bb5q A5q G5q F5q | Bb5q C6q D6q Eb6q | " +
//                "(D6+Eb6)w | (C6+D6)w | (Bb5+C6)w | (A5+Bb5)w | " +
//                "F5q G5q A5q Bb5q | F5q G5q A5q Bb5q | F5h. G5s*3 A5s*3 Bb5s*3 | F5h. G5s*3 A5s*3 Bb5s*3 | F5w " +
//
//                "V1 I[Orchestral_Strings] :Controller(10,10) :Controller(7,80)" +
//                // 和声体系重构(适配Bb大调)
//                "(Bb2+F4+Bb4)w | (Eb3+G4+Bb4)w | (F3+A4+C5)w | (Bb2+F4+Bb4)w | " +
//                "(D3+F4+A4)w | (G3+Bb4+D5)w | (C3+Eb4+G4)w | (F3+A4+C5)w | " +
//                "(Eb3+G4+Bb4)h (Eb3+Ab4+C5)h | (D3+F4+A4)h (G3+Bb4+D5)h | " +
//                "(Bb2+F4+Bb4)w | (Eb3+G4+Bb4)w | (F3+A4+C5)w | (Bb2+D5+F5)w | " +
//                "(Ab3+C5+Eb5)q*4 | (G3+Bb4+D5)q*4 | (F3+A4+C5)q*4 | (Eb3+G4+Bb4)q*4 | " +
//                ":Controller(1,80) (F2+C4+F4)w :Controller(7,50) (Bb2+F4)w :Controller(7,30) (F2)w");

        Pattern composition = new Pattern(
                // 左声道弦乐 (V1) 保持原结构
                "V1 I[Orchestral_Strings] T48 Controller(10,10) Controller(7,75) Controller(1,80) " +
                        "(C3+Eb4+G4)h (C3+Eb4+G4)h (G3+Bb4+D5)h (G3+Bb4+D5)h (C3+Eb4+A4)q. A3s A3s A3s " +
                        "(F3+Ab4+C5)h (F3+Ab4+C5)h (C3+Eb4+G4)h (C3+Eb4+G4)h (G3+Bb4+E5)q. E3s E3s E3s " +
                        "C4w (Eb4+G4+Bb4)q (Eb4+G4+Bb4)q (Eb4+G4+Bb4)q (Eb4+G4+Bb4)q " +
                        "C4w (F4+Ab4+C5)q (F4+Ab4+C5)q (F4+Ab4+C5)q (F4+Ab4+C5)q " +
                        "C4w (D4+F4+A4)q (D4+F4+A4)q (D4+G4+Bb4)q (D4+G4+Bb4)q " +
                        "(G3+Bb4+D5+F5)h (G3+Bb4+D5+F5)h (C4+Eb4+G4+Bb4)h (C4+Eb4+G4+Bb4)h " +
                        "(Ab3+C5+Eb5)q (Ab3+C5+Eb5)q (Ab3+C5+Eb5)q (Ab3+C5+Eb5)q " +
                        "Controller(1,80) (G2+B3+D4+F4)h. Controller(7,50) (G2+B3+D4)h. Controller(7,30) (G2+B3)w " +
                        "Controller(91,30) Controller(93,40)"

        ).add(
                // 右声道钢琴 (V2)
                "V2 I[Piano] T48 Controller(10,120) Controller(7,60) Controller(1,40) " +
                        "G4h Eb5h Bb4h D5h A4q. C5s C5s C5s " +
                        "C5h Ab5h G4h Bb4h E5q. G4s G4s G4s " +
                        "Eb4w (G4+Bb4)q (G4+Bb4)q (G4+Bb4)q (G4+Bb4)q " +
                        "Ab4w (C5+Eb5)q (C5+Eb5)q (C5+Eb5)q (C5+Eb5)q " +
                        "F4w (A4+C5)q (A4+C5)q (Bb4+D5)q (Bb4+D5)q " +
                        "D5h F5h Bb4h Eb5h C5q Eb5q G5q Bb5q " +
                        "(G2+B3+D4)h. (G2+B3)h. G2w"

        ).add(
                // 中置竖琴 (V3) 修正版
                "V3 I46 T48 Controller(10,64) Controller(7,45) " +
                        // 前奏段 (使用琶音写法)
                        "(C5q+Eb5q+G5q) (C5q+Eb5q+G5q) (D5q+F5q+Bb5q) (D5q+F5q+Bb5q) (A4q+C5q+Eb5q)q. Rs Rs Rs " +
                        "(C5q+F5q+Ab5q) (C5q+F5q+Ab5q) (G4q+Bb4q+Eb5q) (G4q+Bb4q+Eb5q) (E4q+G4q+B4q)q. Rs Rs Rs " +
                        // 间奏段 (长音分解)
                        "Eb4w (G4q+Bb4q) (G4q+Bb4q) (G4q+Bb4q) (G4q+Bb4q) " +
                        "F4w (Ab4q+C5q) (Ab4q+C5q) (Ab4q+C5q) (Ab4q+C5q) " +
                        "D4w (F4q+A4q) (F4q+A4q) (G4q+Bb4q) (G4q+Bb4q) " +
                        // 高潮段 (密集分解和弦)
                        "(Bb4q+D5q+F5q)h (Bb4q+D5q+F5q)h (Eb5q+G5q+Bb5q)h (Eb5q+G5q+Bb5q)h " +
                        "(C5q+Eb5q+Ab5q)q (C5q+Eb5q+Ab5q)q (C5q+Eb5q+Ab5q)q (C5q+Eb5q+Ab5q)q " +
                        // 结尾段 (渐弱处理)
                        "(D4q+F4q+B4q)h. (B3q+D4q)h. (G3q+B3q)w"
        ).getPattern();

        Pattern composition1 = new Pattern(
                // 主弦乐声部 (V1)
                "V1 I[Orchestral_Strings] T48 Controller(10,10) Controller(7,40) Controller(91,30) Controller(93,40) " +
                        // 新增前奏
                        "(G2+B3+D4)wwww " +  // 4小节长音

                        // 原前奏段
                        "Controller(7,75) (C3+Eb4+G4)h (C3+Eb4+G4)h (G3+Bb4+D5)h (G3+Bb4+D5)h (C3+Eb4+A4)q. A3s A3s A3s " +
                        "(F3+Ab4+C5)h (F3+Ab4+C5)h (C3+Eb4+G4)h (C3+Eb4+G4)h (G3+Bb4+E5)q. E3s E3s E3s " +

                        // 间奏扩展
                        "C4w (Eb4+G4+Bb4)q (Eb4+G4+Bb4)q (Eb4+G4+Bb4)q (Eb4+G4+Bb4)q " +
                        "C4w (F4+Ab4+C5)q (F4+Ab4+C5)q (F4+Ab4+C5)q (F4+Ab4+C5)q " +
                        "C4w (D4+F4+A4)q (D4+F4+A4)q (D4+G4+Bb4)q (D4+G4+Bb4)q " +

                        // 过渡段
                        "Controller(7,60) (C4+Eb4+Ab4)h (Bb3+D4+G4)h (A3+C#4+F4)h (G3+B3+E4)h " +

                        // 高潮段
                        "Controller(7,80) (G3+Bb4+D5+F5)h (G3+Bb4+D5+F5)h (C4+Eb4+G4+Bb4)h (C4+Eb4+G4+Bb4)h " +
                        "(Ab3+C5+Eb5)q (Ab3+C5+Eb5)q (Ab3+C5+Eb5)q (Ab3+C5+Eb5)q " +

                        // 结尾处理
                        "Controller(1,80) (G2+B3+D4+F4)h. Controller(7,50) (G2+B3+D4)h. " +
                        "Controller(7,30) (G2+B3)w"

        ).add(
                // 钢琴声部 (V2)
                "V2 I[Piano] T48 Controller(10,120) Controller(7,30) Controller(1,40) " +
                        // 前奏呼应
                        "Rwwww " +  // 4小节休止

                        // 主旋律
                        "G4h Eb5h Bb4h D5h A4q. C5s C5s C5s " +
                        "C5h Ab5h G4h Bb4h E5q. G4s G4s G4s " +
                        "Eb4w (G4+Bb4)q (G4+Bb4)q (G4+Bb4)q (G4+Bb4)q " +
                        "Ab4w (C5+Eb5)q (C5+Eb5)q (C5+Eb5)q (C5+Eb5)q " +
                        "F4w (A4+C5)q (A4+C5)q (Bb4+D5)q (Bb4+D5)q " +

                        // 过渡段
                        "D5h F5h Bb4h Eb5h C5q Eb5q G5q Bb5q " +

                        // 尾声
                        "Controller(91,60) (G2+B3+D4)h. (G2+B3)h. G2wwww"

        ).add(
                // 竖琴声部 (V3) 修正版
                "V3 I46 T48 Controller(10,64) Controller(7,25) " +
                        // 前奏涟漪
                        "(C5q+Eb5q+G5q) (Eb5q+G5q+Bb5q) (G5q+Bb5q+D6q) (Bb5q+D6q+F6q) Rww " +

                        // 主结构
                        "(C5q+Eb5q+G5q) (C5q+Eb5q+G5q) (D5q+F5q+Bb5q) (D5q+F5q+Bb5q) (A4q+C5q+Eb5q)q. Rs Rs Rs " +
                        "(C5q+F5q+Ab5q) (C5q+F5q+Ab5q) (G4q+Bb4q+Eb5q) (G4q+Bb4q+Eb5q) (E4q+G4q+B4q)q. Rs Rs Rs " +
                        "Eb4w (G4q+Bb4q) (G4q+Bb4q) (G4q+Bb4q) (G4q+Bb4q) " +
                        "F4w (Ab4q+C5q) (Ab4q+C5q) (Ab4q+C5q) (Ab4q+C5q) " +
                        "D4w (F4q+A4q) (F4q+A4q) (G4q+Bb4q) (G4q+Bb4q) " +

                        // 过渡装饰
                        "(Bb4q+D5q+F5q)h (Bb4q+D5q+F5q)h (Eb5q+G5q+Bb5q)h (Eb5q+G5q+Bb5q)h " +

                        // 尾声残响
                        "(C5w+Eb5w+G5w)q (C5w+Eb5w+G5w)q (C5w+Eb5w+G5w)q Rww"
        ).getPattern();
        Pattern conbia = composition.add(composition1);
//        player.play(conbia);


//        player.play(v1);
        System.out.println("Time: " + (System.currentTimeMillis() - start)/60000d);
        MidiFileManager.savePatternToMidi(conbia, new File("conbia.mid"));
    }
}
